import java.util.List;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

public class levelOrder {
    public List<List<Integer>> _levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        dfs(root, 0, res);

        return res;
    }

    private void dfs(TreeNode node, int level, List<List<Integer>> list) {
        if (node == null) {
            return;
        }
        // 判断是哪一层的节点
        if (list.size() <= level)
            list.add(new ArrayList<>());
        list.get(level).add(node.val);
        dfs(node.left, level + 1, list);
        dfs(node.right, level + 1, list);

    }
}
